$.ajax({
    async: true,
    type: "GET",
    data: {"curr": 1, "username": $("#username").val(), "userrole": $("#userrole").val()},
    url: "/user/getList",
    success: function (data) {
        if (data != null) {
            $('#table tr:gt(0)').remove();
            for (let i = 0; i < data.length; i++) {
                var c = '<tr>\n' +
                    '                <td><span>' + data[i].userCode + '</span></td>\n' +
                    '                <td><span>' + data[i].userName + '</span></td>\n' +
                    '                <td><span>';
                if (parseInt(data[i].gender == 1)) {
                    c += "女";
                } else {
                    c += "男";
                }
                c += '</span></td>\n' +
                    '                <td><span>' + byage(formatDate(new Date(data[i].birthday))) + '</span></td>\n' +
                    '                <td><span>' + data[i].phone + '</span></td>\n' +
                    '                <td><span>' + data[i].roleName + '</span></td>\n' +
                    '                <td>\n' +
                    '                    <span><a class="viewUser" href="/user/view?userid=' + data[i].id + '"><img\n' +
                    '                            src="/statics/images/read.png" alt="查看" title="查看"/></a></span>\n' +
                    '                    <span><a class="modifyUser" href="/user/modify?userid=' + data[i].id + '"><img\n' +
                    '                            src="/statics/images/xiugai.png" alt="修改" title="修改"/></a></span>\n' +
                    '                    <span><a class="deleteUser" href="/user/delete/?userid=' + data[i].id + '"><img\n' +
                    '                            src="/statics/images/schu.png" alt="删除" title="删除"/></a></span>\n' +
                    '                </td>\n' +
                    '            </tr>';
                $("#table").append(c);
            }
        }
        $.ajax({
            async: true,
            type: "GET",
            data: {"curr": 1, "username": $("#username").val(), "userrole": $("#userrole").val()},
            url: "/user/getPage",
            success: function (pageInfo) {
                $("#total").text(pageInfo.totals);
                $("#curr").text(pageInfo.pageNum);
                $("#pages").text(pageInfo.pagess)
            }
        });
    }
});

function getData(curr) {
    $.ajax({
        async: true,
        type: "POST",
        data: {"curr": curr, "username": $("#username").val(), "userrole": $("#userrole").val()},
        url: "/user/getPage",
        success: function (pageInfo) {
            if (pageInfo != null) {
                $("#total").text(pageInfo.totals);
                $("#curr").text(pageInfo.pageNum);
                $("#pages").text(pageInfo.pagess);
                $.ajax({
                    async: true,
                    type: "POST",
                    data: {"curr": curr, "username": $("#username").val(), "userrole": $("#userrole").val()},
                    url: "/user/getList",
                    success: function (data) {
                        if (data != null) {
                            $('#table tr:gt(0)').remove();
                            for (let i = 0; i < data.length; i++) {
                                var c = '<tr>\n' +
                                    '                <td><span>' + data[i].userCode + '</span></td>\n' +
                                    '                <td><span>' + data[i].userName + '</span></td>\n' +
                                    '                <td><span>'
                                if (parseInt(data[i].gender == 1)) {
                                    c += "女";
                                } else {
                                    c += "男";
                                }
                                c += '</span></td>\n' +
                                    '                <td><span>' + byage(formatDate(new Date(data[i].birthday))) + '</span></td>\n' +
                                    '                <td><span>' + data[i].phone + '</span></td>\n' +
                                    '                <td><span>' + data[i].roleName + '</span></td>\n' +
                                    '                <td>\n' +
                                    '                    <span><a class="viewUser" href="/user/view?userid=' + data[i].id + '"><img\n' +
                                    '                            src="/statics/images/read.png" alt="查看" title="查看"/></a></span>\n' +
                                    '                    <span><a class="modifyUser" href="/user/modify?userid=' + data[i].id + '"><img\n' +
                                    '                            src="/statics/images/xiugai.png" alt="修改" title="修改"/></a></span>\n' +
                                    '                    <span><a class="deleteUser" href="/user/delete/?userid=' + data[i].id + '"><img\n' +
                                    '                            src="/statics/images/schu.png" alt="删除" title="删除"/></a></span>\n' +
                                    '                </td>\n' +
                                    '            </tr>';
                                $("#table").append(c);
                                console.log(new String(data[i].birthday))
                            }
                        }
                    }
                });
            }
        }

    });
}

function next() {
    var curr = parseInt($("#curr").text());
    if (curr + 1 > parseInt($("#pages").text())) {
        alert("最后一页");
    } else {
        getData(curr + 1);
    }
}

function pre() {
    var curr = parseInt($("#curr").text());
    if (curr - 1 == 0) {
        alert("第一页");
    } else {
        getData(curr - 1);
    }
}

function lastPage() {
    getData(parseInt($("#pages").text()));
}

function jumpPage() {
    var curr = parseInt($("#inputPage").val());
    if (curr <= 0 || curr > parseInt($("#pages").text())) {
        alert("请输入正确的页数");
    } else {
        getData(curr);
    }
}

function search() {
    $.ajax({
        async: true,
        type: "GET",
        data: {"curr": 1, "username": $("#username").val(), "userrole": $("#userrole").val()},
        url: "/user/getPage",
        success: function (pageInfo) {
            $("#total").text(pageInfo.totals);
            $("#curr").text(pageInfo.pageNum);
            $("#pages").text(pageInfo.pagess)
        }
    });
    getData(1);
}

function byage(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if (nowYear == birthYear) {
        returnAge = 0;//同年 则为0岁
    } else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay;//日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            } else {
                var monthDiff = nowMonth - birthMonth;//月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            }
        } else {
            returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
        }
    }

    return returnAge;//返回周岁年龄
}

function formatDate(now) {
    var year = now.getFullYear();  //取得4位数的年份
    var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
    var date = now.getDate();      //返回日期月份中的天数（1到31）
    return year + "-" + month + "-" + date;
}

$.ajax({
    async: true,
    type: "get",//请求类型
    url: "/role/roleList",//请求的url
    success: function (data) {//data：返回数据（json对象）
        if (data != null) {
            console.log(data)
            var options = "<option value=\"\">请选择</option>";
            for (var i = 0; i < data.length; i++) {
                //alert(data[i].id);
                //alert(data[i].roleName);
                options += "<option value=\"" + data[i].id + "\">" + data[i].rolename + "</option>";
            }
            $("#userrole").append(options);
        }
    }

});